package com.school.base.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

import com.school.base.dao.IStudentDao;
import com.school.base.model.Student;
import com.school.common.base.BaseDao;
import com.school.common.constant.Constant;
import com.school.common.util.MapUtils;
import com.school.common.util.Md5Utils;

public class StudentDao extends BaseDao implements IStudentDao {

	@Override
	public Student getStudentById(int id) {
		return (Student) this.getObjectById(Student.class, id);
	}

	private String getExpressSql(Student student, List express) {
		String sql = "";
		if (student != null) {
			if (student.getRealName() != null && !student.getRealName().isEmpty()) {
				sql += " and s.real_name like ?";
				express.add("%" + student.getRealName() + "%");
			}
			if (student.getClassId() > 0) {
				sql += " and exists(select * from b_class_student cs where cs.student_id=s.id and cs.class_id=?)";
				express.add(student.getClassId());
			}
		}
		return sql;
	}

	@Override
	public List<Student> getStudentList(Student student, int offset, int pageSize) {
		String sql = "select * from b_student s where s.status<>" + Constant.ACCOUNT_STATUS_DELETED;
		List express = new ArrayList();
		sql += this.getExpressSql(student, express);
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				query.setParameter(i, express.get(i));
			}
		}
		query.setMaxResults(pageSize);
		query.setFirstResult(offset);
		List list = query.list();
		List<Student> studentList = new ArrayList<Student>();
		for (Object obj : list) {
			Student s = (Student) MapUtils.mapToBean((HashMap<String, Object>) obj, Student.class);
			studentList.add(s);
		}
		return studentList;
	}

	@Override
	public int getTotalCount(Student student) {
		String sql = "select count(*) from b_student s where s.status<>" + Constant.ACCOUNT_STATUS_DELETED;
		List express = new ArrayList();
		sql += this.getExpressSql(student, express);
		return this.getRecordCountBySql(sql, express);
	}

	@Override
	public void updateStudent(Student student) {
		String hql = "update Student set ";
		List express = new ArrayList();
		if (student.getPwd() != null && !student.getPwd().isEmpty()) {
			student.setPwd(Md5Utils.getMd5(student.getPwd()));
			hql += "pwd=?, ";
			express.add(student.getPwd());
		}
		hql += "realName=?, ";
		express.add(student.getRealName());
		hql += "gender=?, ";
		express.add(student.getGender());
		hql += "introduction=?, ";
		express.add(student.getIntroduction());
		hql += "birthday=?, ";
		express.add(student.getBirthday());
		hql += "identityNum=?, ";
		express.add(student.getIdentityNum());
		hql += "telPhone=?, ";
		express.add(student.getTelPhone());
		if (student.getAvatarSmall() == null || student.getAvatarSmall().isEmpty()) {
			student.setAvatarSmall(Constant.STUDENT_AVATAR_SMALL_PATH);
		}
		hql += "avatarSmall=?, ";
		express.add(student.getAvatarSmall());
		if (student.getAvatarLarge() == null || student.getAvatarLarge().isEmpty()) {
			student.setAvatarLarge(Constant.STUDENT_AVATAR_LARGE_PATH);
		}
		hql += "avatarLarge=?, ";
		express.add(student.getAvatarLarge());
		hql += "oicqNum=?, ";
		express.add(student.getOicqNum());
		// last--------
		hql += "email=? ";
		express.add(student.getEmail());

		hql += "where id=?";
		express.add(student.getId());
		this.executeHql(hql, express);
	}

	@Override
	public void deleteStudent(int id) {
		String hql = "update Student set status=" + Constant.ACCOUNT_STATUS_DELETED + " where id=" + id;
		this.executeHql(hql);
	}

	@Override
	public void insertStudent(Student student) {
		student.setStatus(Constant.ACCOUNT_STATUS_INUSED);
		student.setPwd(Md5Utils.getMd5(student.getPwd()));
		this.insert(student);
		String sql = "insert b_class_student (class_id,student_id) values (?,?)";
		List express = new ArrayList();
		express.add(student.getClassId());
		express.add(student.getId());
		this.executeSql(sql, express);
	}

	@Override
	public boolean checkLoginName(String loginName, int studentId) {
		List<Criterion> express = new ArrayList<Criterion>();
		express.add(Restrictions.eq("loginName", loginName));
		express.add(Restrictions.ne("id", studentId));
		List list = this.getList(Student.class, express, null);
		if (list != null && !list.isEmpty()) {
			return false;
		}
		return true;
	}

}
